<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FangOwnerExport;
use App\Models\FangOwner;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class FangOwnerController extends BaseController
{
    /**
     * 房东信息列表显示
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $data = FangOwner::paginate($this->pagesize)->toArray();
        $data = $data['data'];
        // dd($data);
        return view('admin.fangowner.index', compact('data'));
    }

    /**
     * 房东添加页面显示
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('admin.fangowner.create');
    }

    /**
     * @param Request $request
     * @return array
     */
    public function store(Request $request)
    {
        try {
            $data = $this->validate($request, [
                'name'    => 'required|unique:fang_owners,name',
                'gender'  => 'required',
                'age'     => 'required',
                'phone'   => 'required',
                'card'    => 'required',
                'address' => 'required',
                'pic'     => 'required',
                'pic1'    => 'required',
                'pic2'    => 'required',
                'email'   => 'nullable',
            ]);
        } catch (\Exception $e) {
            return ['msg' => '验证不通过', 'status' => 404];
        }
        FangOwner::create($data);
        return ['msg' => '添加节点成功', 'status' => 200];
    }

    /**
     * Display the specified resource.
     *
     * @param FangOwner $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function show(FangOwner $fangOwner)
    {
        //
    }

    /**
     * @param Request $request
     * @param FangOwner $fangowner
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request, FangOwner $fangowner)
    {
        return view('admin.fangowner.edit', compact('fangowner'));
    }

    /**
     * @param Request $request
     * @param FangOwner $fangowner
     * @return array
     */
    public function update(Request $request, FangOwner $fangowner)
    {
        try {
            $data = $this->validate($request, [
                'name'    => 'required',
                'gender'  => 'required',
                'age'     => 'required',
                'phone'   => 'required',
                'card'    => 'required',
                'address' => 'required',
                'pic'     => 'required',
                'pic1'    => 'required',
                'pic2'    => 'required',
                'email'   => 'nullable',
            ]);
        } catch (\Exception $e) {
            return ['msg' => '验证不通过', 'status' => 404];
        }
        $fangowner->update($data);
        return ['msg' => '添加节点成功', 'status' => 200];
    }

    /**
     * @param FangOwner $fangowner
     * @return array
     */
    public function destroy(FangOwner $fangowner)
    {
        //dd($fangowner);
        $fangowner->delete();
        return ['msg' => '删除成功', 'status' => 200];
    }

    // upimg
    public function upimg(Request $request)
    {
        $pic = config('uploadpic.img');
        if ($request->hasFile('file')) {
            $info = $request->file('file')->store('', 'fangattr');
            $pic  = '/uploads/fangattr/images/' . $info;
        }
        return ['code' => 200, 'src' => $pic];
    }

    // delimg
    public function delimg(Request $request)
    {
        $src = public_path() . $request->get('src');
        unlink($src);
    }

    //
    public function showpic(Request $request)
    {
        // dd($fangOwner);
        $fangOwner = $request->all();
        return view('admin.fangowner.show', compact('fangOwner'));
    }

    // export
    public function export(){
        return Excel::download(new FangOwnerExport(), '房东信息.xlsx');
    }
}
